<template>
  <div class="count">
    <h1>当前的和为：{{sum}}</h1>
    <select v-model.number="n">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <button @click="incrementIfOdd">当前和为奇数再加</button>
    <button @click="incrementWait">等一等再加</button>
  </div>
</template>

<script>
  export default {
    name:'Count',
    data() {
        return {
            sum:0,
            n:1
        }
    },
    methods: {
        // 点击【+】按钮的回调
        increment(){
            this.sum += this.n
        },
         // 点击【-】按钮的回调
        decrement(){
            this.sum -= this.n
        },
        // 点击【当前和为奇数再加】按钮的回调
        incrementIfOdd(){
            if(this.sum % 2){
                this.sum += this.n
            }
        },
        // 点击【等一等再加】按钮的回调
        incrementWait(){
            setTimeout(()=>{
                this.sum += this.n
            },1000)
        }
    },
  }
</script>

<style scoped>
  .count {
    background-color: skyblue;
    padding: 20px;
  }
  button {
    margin-left: 10px;
  }
</style>